package 字符串;

import java.util.Stack;

public class LeetCode844_比较含退格的字符串 {
    public boolean backspaceCompare(String s, String t) {
        Stack<Character> sS = new Stack<Character>();
        Stack<Character> tS = new Stack<Character>();

        for(int i = 0; i < s.length(); i++) {
            if(i == 0 && s.charAt(i) == '#') {
                continue;
            }else if(sS.isEmpty() && s.charAt(i) == '#') {
                continue;
            }else if(s.charAt(i) != '#') {
                sS.push(s.charAt(i));
            }else {
                sS.pop();
            }
        }

        for(int i = 0; i < t.length(); i++) {
            if(i == 0 && t.charAt(i) == '#') {
                continue;
            }else if(tS.isEmpty() && t.charAt(i) == '#') {
                continue;
            }else if(t.charAt(i) != '#') {
                tS.push(t.charAt(i));
            }else {
                tS.pop();
            }
        }

        if(sS.size() == tS.size()) {
            if(sS.size() == 0 && tS.size() == 0) {
                return true;
            }
            while(!sS.isEmpty()) {
                if(sS.pop() != tS.pop()) {
                    return false;
                }
            }
            return true;
        }
        return false;
    }
}
